package com.yituo.mjworld.mvc.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.yituo.mjworld.core.web.core.BaseForm;
import com.yituo.mjworld.core.web.core.Result;
import com.yituo.mjworld.mvc.dao.ChildItemDao;
import com.yituo.mjworld.mvc.entity.web.WebChildItem;
import com.yituo.mjworld.mvc.form.DeleteForm;
import com.yituo.mjworld.mvc.form.QueryForm;
import com.yituo.mjworld.mvc.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 二级分类操作接口
 *
 * @author xq
 */
@Service
public class ChildItemServiceImpl extends BaseService {

  @Autowired
  ChildItemDao childItemDao;

  @Autowired
  QiniuServiceImpl qiniuService;

  /**
   * 查询二级分类
   *
   * @param form
   * @return
   */
  @Override
  public JSONObject getList(QueryForm form) {
    String countSql = "select count(id) from web_child_item where parentId=:parentId ";
    String sql = "select id,parentId,title,orderNum,imgUrl from web_child_item where parentId=:parentId order by orderNum ";
    form.getParam().remove("page");
    form.getParam().remove("rows");
    Map<String, Object> params = form.getParam();
    return this.get(sql, countSql, form, params);
  }

  /**
   * 删除二级分类
   *
   * @param form 欲删除的父级别分类ID
   * @return
   */
  @Override
  public Result delete(DeleteForm form) {
    Result result = new Result();
    String sql = "select count(id) from product where childId=:childId";
    Map<String, Object> param = new HashMap<String, Object>();
    param.put("childId", form.getId());
    long childNum = baseDao.countBySql(sql, param);
    if (childNum == 0) {
      WebChildItem item = (WebChildItem) baseDao.get(WebChildItem.class, form.id);
      if (item == null) {
        result.result = false;
        result.msg = "您删除的类别不存在!";
      } else {
        // 删除
        baseDao.delete(WebChildItem.class, form.getId());
        result.result = true;
      }
    } else {
      result.result = false;
      result.msg = "您删除的二级分类下还有商品!请先删除对应的商品后后再删除该分类!";
    }
    return result;
  }

  /**
   * 保存或者更新
   *
   * @param form 欲添加的二级分类的item
   * @return
   */
  @Override
  public Result saveOrUpdate(BaseForm form) {
    Result result = new Result();
    WebChildItem item = (WebChildItem) form;
    WebChildItem item1 = (WebChildItem) baseDao.get(WebChildItem.class, item.getId());
    WebChildItem item2 = childItemDao.getChildItemByTitle(item.getTitle());
    if (item1 == null) {
      // 新增操作
      if (item2 == null) {
        // 不存在重复的标题
        baseDao.saveOrUpdate(item);
        result.setResult(true);
      } else {
        result.setMsg("重复的标题").setResult(false);
      }
    } else {
      if (item2 != null && item2.getId() != item.getId() && !StringUtils.isEmpty(item1.getImgUrl()) && !item1.getImgUrl().equals(item.getImgUrl())) {
        // 无重复标题,且两个url不相等,删除原来的url
        qiniuService.delete(qiniuService.getFileNameForUrl(item1.getImgUrl()));
      }
      // update 操作
      if (item2 == null) {
        // 不存在重复标题
        baseDao.merge(item);
        result.setResult(true);
      } else {
        // 存在重复的标题
        if (item.getId() == item2.getId()) {

          // 该重复标题的ID为自己,认为不存在标题
          baseDao.merge(item);
          result.setResult(true);
        } else {
          result.setResult(false).setMsg("重复的标题");
        }
      }
    }
    return result;
  }

  /**
   * 更新URL
   *
   * @param id
   * @param url
   * @return
   */
  public Result updateUrl(int id, String url) {
    WebChildItem item = (WebChildItem) baseDao.get(WebChildItem.class, id);
    if (item == null) {
      return new Result().setResult(false).setMsg("更新的项目不存在!");
    } else {
      if (url.equals(item.getImgUrl())) {
        // 两次更新的内容相等.
        return new Result().setResult(true);
      }
      if (!StringUtils.isEmpty(item.getImgUrl())) {
        // 之前的图片不为空
        qiniuService.delete(qiniuService.getFileNameForUrl(item.getImgUrl()));
      }
      item.setImgUrl(url);
      baseDao.saveOrUpdate(item);
    }
    return new Result().setResult(true);
  }


}
